Imports DAL
Imports System.Configuration
Imports MyClasses
Imports System.IO
Imports System.Windows.Forms.SaveFileDialog
Imports C1.C1PrintDocument
Imports System.Drawing.Printing

Public Class frmPurchaseItemList
    Public btLoadFlag As Byte


    Private Sub frmPurchaseItemList_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Purchase Item List")
        End If
    End Sub
    Private Sub frmPurchaseItemList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btLoadFlag = 0
        Call BCommon.SetFormStandards(Me)
        Call DisplayData()
        fgItemList.Rows.Count = 22
        btLoadFlag = 1
    End Sub

#Region "User Defined Funcitons"
    Sub SetCaption()
        Try
            With fgItemList
                .Cols.Count = 6
                .Cols(0).Visible = False
                .Cols(1).Caption = "Item Name"
                .Cols(1).Width = Width / 4
                .Cols(1).AllowEditing = False

                .Cols(2).Caption = "Current Stock"
                .Cols(2).Width = Width / 7
                .Cols(2).AllowEditing = False
                .Cols(3).Caption = "Near To Expiry"
                .Cols(3).Width = Width / 7
                .Cols(3).AllowEditing = False
                .Cols(4).Caption = "Reorder Quantity"
                .Cols(4).Width = Width / 7
                .Cols(4).AllowEditing = True
                If btLoadFlag <> 0 Then .Cols(5).DataType = System.Type.GetType("System.Boolean")
                .Cols(5).Caption = "Order"
                .Cols(5).Width = Width / 10
                .Cols(5).AllowEditing = True
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetCaption")
        End Try
    End Sub

    Private Sub DisplayData()
        Try
            TxtReOrder.Text = String.Empty
            BCommon.SetComboValues(cmbVendor, BVendor.GetVendor(), "VendorID", "VendorName")
            Call SetCaption()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
        End Try
    End Sub
    Private Sub FillData()

        Try
            Dim dtItems As DataTable
            If ValidateData() = 0 Then Exit Sub
            With fgItemList
                dtItems = BPurchaseItemList.GetItemList(IIf(ChkAll.Checked, 0, cmbVendor.SelectedValue), Trim(TxtReOrder.Text))
                If dtItems.Rows.Count > 0 Then
                    .DataSource = dtItems
                    Call SetCaption()
                    .Cols(5).DataType = GetType(Boolean)
                    .Cols(5).ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.CenterCenter
                    .Cols(4).AllowEditing = True
                    .Cols(5).AllowEditing = True
                Else
                    btLoadFlag = 0
                    fgItemList.Clear()
                    fgItemList.DataSource = Nothing
                    Call SetCaption()
                    fgItemList.Rows.Count = 22
                    btLoadFlag = 1
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "FillData")
        End Try
    End Sub
    Private Function ValidateData() As Byte
        Try
            If TxtReOrder.Text <> Trim(String.Empty) And Not IsNumeric(TxtReOrder.Text) Then
                BCommon.ShowMsgError(10239)
                TxtReOrder.Focus()
                TxtReOrder.Select(0, TxtReOrder.TextLength)
                Return 0
            ElseIf cmbVendor.SelectedIndex <= 0 Then
                BCommon.ShowMsgError(10274)
                cmbVendor.Focus()
                Return 0
            End If
            Return 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Validatedata")
        End Try
    End Function
    Private Sub WritePDF()
        Try

            Dim IntCount As Integer
            Dim btChk As Byte

            With fgItemList
                If (Not IsNothing(.Rows(1).Item(1)) OrElse .Rows(1).Item(1) <> "") Then
                    If ((.Rows.Count) - 1) > 0 Then
                        For IntCount = 1 To .Rows.Count - 1
                            If (.Item(IntCount, 1) <> String.Empty) And (Convert.ToBoolean(.Item(IntCount, 5) = False)) Then
                                btChk = 1
                            Else
                                btChk = 0
                                Exit For
                            End If
                        Next
                        If btChk = 1 Then
                            BCommon.ShowMsgError(10287)
                            Exit Sub
                        End If
                    End If
                    Call PrintPDF() 'Calling Function For Printing The Selected Items.
                Else
                    BCommon.ShowMsgError(10402)
                    btnOrder.Focus()
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "WritePDF")
        End Try
    End Sub
    Private Sub PrintPDF()
        Dim strFilePath As String
        ''Dim objdoc As New C1PrintDocument
        Dim Discrp As String = String.Empty
        Dim FirmName As String = String.Empty
        Dim PinCode As String = String.Empty
        Dim addDesc1 As String = String.Empty
        Dim phone1 As String = String.Empty
        Dim i As Integer
        Try
            Dim objRst As System.Windows.Forms.DialogResult

            With filedlg
                objRst = .ShowDialog()
                strFilePath = .FileName
            End With

            If strFilePath = "" And objRst = Windows.Forms.DialogResult.OK Then
                'MessageBox.Show("Please enter the file name", "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                BCommon.ShowMsgError(10386)
                Exit Sub
            ElseIf objRst = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If

            With fgItemList
                .Rows(0).Selected = True
                .SaveGrid("d:\TempList.txt", C1.Win.C1FlexGrid.FileFormatEnum.TextTab)
            End With

            '''''''''''''''''''''
            ''Dim fgTempList As New C1.Win.C1FlexGrid.C1FlexGrid
            ''With fgTempList
            ''    .LoadGrid("d:\TempList.txt", C1.Win.C1FlexGrid.FileFormatEnum.TextTab)
            ''    .Cols(0).Visible = False

            ''    For i = 1 To .Rows.Count - 1
            ''        If Not IsNothing(.Item(i, 5)) AndAlso Trim(.Item(i, 5)) <> "" Then
            ''            If .Item(i, 5) = True Then
            ''                .Rows(i).Visible = True
            ''            Else
            ''                .Rows(i).Visible = False
            ''            End If
            ''        Else
            ''            .Rows(i).Visible = False
            ''        End If
            ''    Next
            ''    .Rows.Count = fgItemList.Rows.Count + 1
            ''    .Cols(3).Visible = False
            ''    .Cols(5).Visible = False
            ''    .Cols(2).Visible = False
            ''    .Cols.Fixed = 0
            ''    .Cols.Count = 6
            ''    .Cols(1).Caption = "Item Name"
            ''    .Cols(1).Width = Width / 4
            ''    .Cols(1).AllowEditing = False
            ''    .Cols(2).Caption = "Current Stock"
            ''    .Cols(2).Width = Width / 6
            ''    .Cols(2).AllowEditing = False
            ''    .Cols(3).Caption = "Near To Expiry"
            ''    .Cols(3).Width = Width / 6
            ''    .Cols(3).AllowEditing = False
            ''    .Cols(4).Caption = "Quantity"
            ''    .Cols(4).Width = Width / 6
            ''    .Cols(4).AllowEditing = True
            ''    .Cols(5).DataType = System.Type.GetType("System.Boolean")
            ''    .Cols(5).Caption = "Order"
            ''    .Cols(5).Width = Width / 10
            ''    .Cols(5).AllowEditing = True
            ''End With
            '''''''''''''''''''''
            Dim dtFirm As DataTable
            dtFirm = BCommon.FirmInfo()
            If dtFirm.Rows.Count > 0 Then
                Discrp = Trim(dtFirm.Rows(0).Item("Description").ToString())
                FirmName = Trim(dtFirm.Rows(0).Item("FirmName").ToString())
                PinCode = Trim(dtFirm.Rows(0).Item("PinCode").ToString())
                addDesc1 = Trim(dtFirm.Rows(0).Item("addDesc1").ToString())
                phone1 = Trim(dtFirm.Rows(0).Item("phone1").ToString())
            End If
            ''Dim doc As New C1.C1PrintDocument.C1DocStyle(objdoc)
            ''doc.BackColor = Color.White
            ''doc.TextColor = Color.Black
            ''With objdoc
            ''    .StartDoc()
            ''    .StyleTableCell = doc
            ''    .RenderDirectText(2.5, 0.2, FirmName, Nothing, Nothing, Color.Black, AlignHorzEnum.Center)
            ''    .RenderDirectText(1.1, 0.6, addDesc1 & "," & Discrp & "-" & PinCode & ", Tel: " & phone1, objdoc.BodyAreaSize.Width, Nothing, Color.Black, AlignHorzEnum.Left)
            ''    .RenderDirectText(1.1, 0.9, "Vendor Name : " & cmbVendor.Text.ToString(), objdoc.BodyAreaSize.Width, Nothing, Color.Black, AlignHorzEnum.Left)
            ''    .RenderDirectText(3.9, 0.9, "Date:" & Date.Now.Date(), objdoc.BodyAreaSize.Width, Nothing, Color.Black, AlignHorzEnum.Left)
            ''    .RenderDirectC1Printable(1.1, 1.8, fgTempList, objdoc.BodyAreaSize.Width, Nothing, Nothing, doc)
            ''    .StyleTableCell = doc
            ''    .EndDoc()
            ''    .ExportToPDF(strFilePath, False, True)
            ''    .PageSettings.PaperSize.RawKind = 9
            ''    .PageSettings.Margins.Bottom = 0.3
            ''    .PageSettings.Margins.Top = 0.3
            ''    .PageSettings.Margins.Left = 0.3
            ''    .PageSettings.Margins.Right = 0.3
            ''End With

            Dim pdf As New C1.C1Pdf.C1PdfDocument()
            ' add content to the page
            Dim rc As RectangleF = pdf.PageRectangle
            Dim rc2 As RectangleF = pdf.PageRectangle
            Dim TextSize As New System.Drawing.SizeF
            '' Dim TextSize1 As New System.Drawing.SizeF
            rc.Inflate(-72, -72)
            pdf.PaperKind = PaperKind.A4

            Dim X As Integer = pdf.PageRectangle.Left
            Dim Y As Integer = pdf.PageRectangle.Top
            Dim W As Integer = pdf.PageRectangle.Width
            Dim TW As Integer = pdf.PageRectangle.Width
            W = W - 100
            Y = Y + 100
            X = X + 50
            Dim font As Font = New Font("Arial", 9)
            Dim hfont As Font = New Font("Arial", 12, FontStyle.Bold)
            Dim shfont As Font = New Font("Arial", 9, FontStyle.Bold)
            ' save the document to a file
            'HEADER 
            Dim sf As New StringFormat()
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            TextSize = pdf.MeasureString(FirmName, hfont)

            Dim rc1 As New RectangleF(X, Y, W, TextSize.Height)
            pdf.DrawString(FirmName, hfont, Brushes.Black, rc1, sf)
            Y = Y + TextSize.Height
            rc1 = New RectangleF(X, Y, W, TextSize.Height)

            pdf.DrawString(addDesc1 & "," & Discrp & "-" & PinCode & ", Tel: " & phone1, font, Brushes.Black, rc1, sf)
            Y = Y + TextSize.Height
            TextSize = pdf.MeasureString("Vendor Name :  & cmbVendor.Text.ToString()", font)
            rc2 = New RectangleF(X, Y, TextSize.Width, TextSize.Height)
            pdf.DrawString("Vendor Name : " & cmbVendor.Text.ToString(), font, Brushes.Black, rc2)
            TextSize = pdf.MeasureString("Date: " & Format(Date.Now.Date(), "dd/MM/yy"), font)
            rc1 = New RectangleF(TW - TextSize.Width - 100, Y, TextSize.Width, TextSize.Height)
            pdf.DrawString("Date:" & Format(Date.Now.Date(), "dd/MM/yy"), font, Brushes.Black, rc1)


            Y = Y + TextSize.Height
            TextSize = pdf.MeasureString("Purchase Order", hfont)
            rc1 = New RectangleF(X, Y, W, TextSize.Height)
            Y = Y + TextSize.Height
            pdf.DrawString("Purchase Order", hfont, Brushes.Black, rc1, sf)
            Y = Y + TextSize.Height
            'HEADING LINE 
            pdf.DrawLine(Pens.Black, X, Y, W, Y)
            Dim rc3 As New RectangleF
            '' Dim rc4 As New RectangleF
            TextSize = pdf.MeasureString("S.No.", hfont)
            rc1 = New RectangleF(X, Y, W / 2, TextSize.Height)
            rc2 = New RectangleF(X + (W / 7), Y, W / 5, TextSize.Height)
            rc3 = New RectangleF(X + (W / 1.5), Y, W / 3, TextSize.Height)
            pdf.DrawString("S.No.", shfont, Brushes.Black, rc1)
            pdf.DrawString("Item Name", shfont, Brushes.Black, rc2)
            pdf.DrawString("Quantity", shfont, Brushes.Black, rc3)
            Y = Y + TextSize.Height
            pdf.DrawLine(Pens.Black, X, Y, W, Y)
            'ITEMS 
            Dim sf1 As New StringFormat()
            Dim intSRNO As Integer = 1
            sf1.Alignment = StringAlignment.Far
            sf1.LineAlignment = StringAlignment.Far

            Dim dbltot As Double
            With fgItemList
                For i = 1 To .Rows.Count - 1
                    If Not IsNothing(.Item(i, 5)) AndAlso Trim(.Item(i, 5)) <> "" Then
                        If .Item(i, 5) = True Then
                            rc1 = New RectangleF(X, Y, W / 2, TextSize.Height)
                            'rc2 = New RectangleF(X + (W / 7), Y, W / 5, TextSize.Height)
                            rc2 = New RectangleF(X + (W / 7), Y + 1, W / 2, TextSize.Height)
                            rc3 = New RectangleF(X + (W / 1.5), Y, W - (X + (W / 1.3)), TextSize.Height)
                            pdf.DrawString(intSRNO, font, Brushes.Black, rc1)
                            pdf.DrawString(.Item(i, 1), font, Brushes.Black, rc2)
                            pdf.DrawString(.Item(i, 4), font, Brushes.Black, rc3, sf1)
                            'pdf.DrawRectangle(Pens.BlueViolet, rc3)
                            Y = Y + TextSize.Height
                            dbltot += .Item(i, 4)
                            intSRNO = intSRNO + 1
                        End If
                    End If
                Next
                pdf.DrawLine(Pens.Black, X, Y, W, Y)
                rc2 = New RectangleF(X + (W / 1.7), Y, W / 5, TextSize.Height)
                pdf.DrawString("Total :", font, Brushes.Black, rc2)
                rc3 = New RectangleF(X + (W / 1.5), Y, W - (X + (W / 1.3)), TextSize.Height)
                pdf.DrawString(dbltot, font, Brushes.Black, rc3, sf1)
            End With
            'FOOTER 

            pdf.Save(strFilePath)

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "PrintPDF")
        End Try

    End Sub
#End Region

#Region "Key Down Events"

    Private Sub cmbVendor_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVendor.DropDownClosed
        If btLoadFlag = 0 Then Exit Sub
        If cmbVendor.SelectedIndex <= 0 Then txtVendorID.Text = String.Empty
        If cmbVendor.SelectedValue > 0 Then
            txtVendorID.Text = String.Empty
            txtVendorID.Text = BCommon.GetSequentialCode("VENDOR") & Trim(cmbVendor.SelectedValue.ToString)
        End If
        FillData()
    End Sub

    Private Sub cmbVendor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbVendor.KeyDown
        If btLoadFlag = 0 Then Exit Sub
        If e.KeyCode = Keys.Enter Then
            FillData()
            TxtReOrder.Focus()
        End If
    End Sub

    Private Sub TxtReOrder_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtReOrder.KeyDown
        If BCommon.ValidateText(e.KeyCode, "NUM", False) = False Then
            e.SuppressKeyPress = True
        End If
        If e.KeyCode = Keys.Enter Then
            If ValidateData() = 0 Then Exit Sub
            FillData()
            btnOrder.Focus()
        End If
    End Sub
#End Region
#Region "MouseClickEvents"
    Private Sub btnOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrder.Click
        If ValidateData() = 0 Then Exit Sub
        Call WritePDF()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        btLoadFlag = 0
        Call ClearData()
        Call DisplayData()
        fgItemList.Rows.Count = 22
        btLoadFlag = 1
        ChkAll.Checked = False
    End Sub
    Private Sub ClearData()
        Try
            fgItemList.Clear()
            fgItemList.DataSource = Nothing
            cmbVendor.SelectedIndex = -1
            txtVendorID.Text = String.Empty            
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
        End Try
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
#End Region

    Private Sub cmbVendor_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVendor.LostFocus
        If sender.selectedindex = -1 Then
            cmbVendor.Focus()
            sender.text = String.Empty
        End If
    End Sub

#Region "Grid Events"

    Private Sub fgItemList_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItemList.AfterEdit
        'With fgItemList
        '    Select Case .Col
        '        Case "5"
        '            If Not IsNothing(.Item(.Row, 5)) AndAlso Trim(.Item(.Row, 5)) <> String.Empty Then
        '                .Rows(.Row).Selected = True
        '            Else : .Rows(.Row).Selected = False
        '            End If

        '    End Select
        'End With
    End Sub

    Private Sub fgItemList_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItemList.BeforeEdit
        With fgItemList
            Select Case .Col
                Case "5"
                    If Not IsNothing(.Item(.Row, 5)) AndAlso Trim(.Item(.Row, 5)) <> String.Empty Then
                        .Rows(.Row).Selected = True
                    Else : .Rows(.Row).Selected = False
                    End If

            End Select
        End With
    End Sub

    Private Sub fgItemList_EnterCell(ByVal sender As Object, ByVal e As System.EventArgs) Handles fgItemList.EnterCell
        Try
            With fgItemList
                If btLoadFlag <> 0 Then
                    Select Case .Col
                        Case 1, 2, 3, 4, 5
                            If cmbVendor.SelectedValue <= 0 Then                               
                                BCommon.ShowMsgError(10274)                                
                                cmbVendor.Focus()
                                Exit Sub
                            End If
                    End Select
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItemList_EnterCell")
        End Try
    End Sub

    Private Sub fgItemList_SetupEditor(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItemList.SetupEditor
        If e.Col = 4 Then
            Dim ctl As Control = fgItemList.Editor
            CType(ctl, TextBox).MaxLength = 5
        End If
    End Sub

    Private Sub fgItemList_StartEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItemList.StartEdit
        Try
            With fgItemList
                Select Case .Col
                    Case 5
                End Select

            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub fgItemList_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles fgItemList.ValidateEdit
        Try
            With fgItemList
                If Not IsNothing(fgItemList.Item(e.Row, 1)) Then
                    Select Case e.Col
                        Case 4
                            'If Not IsNothing(fgItemList.Editor) AndAlso Trim(fgItemList.Editor.Text) <> "" Then
                            If Not IsNothing(fgItemList.Editor) AndAlso Trim(fgItemList.Editor.Text) <> "" Then
                                If Not IsNumeric(fgItemList.Editor.Text) Then
                                    BCommon.ShowMsgError(10239)
                                    e.Cancel = True
                                End If
                            Else
                                BCommon.ShowMsgError(10289)
                                e.Cancel = True
                            End If
                        Case 5
                            If e.Checkbox = C1.Win.C1FlexGrid.CheckEnum.Checked Then
                                If fgItemList.Item(e.Row, 4) <= 0 Then
                                    BCommon.ShowMsgError(10324)
                                    e.Cancel = True
                                End If
                            End If
                    End Select
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItemList_ValidateEdit")
        End Try
    End Sub
#End Region

    Private Sub txtVendorID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVendorID.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtVendorID_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVendorID.LostFocus
        Dim intVendId As Integer
        Dim dsVendorId As New DataSet
        Try
            If Me.txtVendorID.Text <> String.Empty Then
                dsVendorId = BVendor.GetVendorId(txtVendorID.Text.ToUpper())
                If dsVendorId.Tables(0).Rows.Count > 0 Then
                    intVendId = Convert.ToInt32(dsVendorId.Tables(0).Rows(0).Item(0).ToString())
                    cmbVendor.SelectedValue = intVendId
                    cmbVendor.Focus()
                Else
                    cmbVendor.SelectedValue = 0
                    'MessageBox.Show(BCommon.GetMessage(10277), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    BCommon.ShowMsgError(10277)
                    txtVendorID.Text = String.Empty
                    Me.txtVendorID.Focus()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtVendorID_LostFocus")
        End Try
    End Sub

    Private Sub ChkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkAll.CheckedChanged
        Call FillData()
    End Sub

    Private Sub TxtReOrder_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtReOrder.LostFocus
        Call FillData()
    End Sub
End Class
